| Conditions | 1 |
| Paths | 2 |
| Total Lines | 124 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 26 | $.fn.symphonyOrderable = function(options) { |
||
| 27 | var objects = this, |
||
| 28 | settings = { |
||
| 29 | items: 'li', |
||
| 30 | handles: '*', |
||
| 31 | ignore: 'input, textarea, select, a', |
||
| 32 | delay: 250 |
||
| 33 | }; |
||
| 34 | |||
| 35 | $.extend(settings, options); |
||
| 36 | |||
| 37 | /*------------------------------------------------------------------------- |
||
| 38 | Events |
||
| 39 | -------------------------------------------------------------------------*/ |
||
| 40 | |||
| 41 | // Start ordering |
||
| 42 | objects.on('mousedown.orderable', settings.items + ' ' + settings.handles, function startOrdering(event) { |
||
| 43 | var handle = $(this), |
||
| 44 | item = handle.parents(settings.items), |
||
| 45 | object = handle.parents('.orderable'); |
||
| 46 | |||
| 47 | // Needed to prevent browsers from selecting texts and focusing textinputs |
||
| 48 | if(!$(event.target).is('input, textarea')) { |
||
| 49 | event.preventDefault(); |
||
| 50 | } |
||
| 51 | |||
| 52 | if(!handle.is(settings.ignore) && !$(event.target).is(settings.ignore)) { |
||
| 53 | object.data('ordering', 1); |
||
| 54 | |||
| 55 | // Highlight item |
||
| 56 | if(object.is('.selectable, .collapsible')) { |
||
| 57 | |||
| 58 | // Delay ordering to avoid conflicts with scripts bound to the click event |
||
| 59 | setTimeout(function() { |
||
| 60 | if(object.data('ordering') == 1) { |
||
|
|
|||
| 61 | object.trigger('orderstart.orderable', [item]); |
||
| 62 | item.addClass('ordering'); |
||
| 63 | } |
||
| 64 | }, settings.delay); |
||
| 65 | } |
||
| 66 | else { |
||
| 67 | object.trigger('orderstart.orderable', [item]); |
||
| 68 | item.addClass('ordering'); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | }); |
||
| 72 | |||
| 73 | // Stop ordering |
||
| 74 | objects.on('mouseup.orderable mouseleave.orderable', function stopOrdering(event) { |
||
| 75 | var object = $(this), |
||
| 76 | item; |
||
| 77 | |||
| 78 | if(object.data('ordering') == 1) { |
||
| 79 | item = object.find('.ordering'); |
||
| 80 | item.removeClass('ordering'); |
||
| 81 | object.data('ordering', 0); |
||
| 82 | object.trigger('orderstop.orderable', [item]); |
||
| 83 | |||
| 84 | // Lock item to avoid conflicts with scripts bound to the click event |
||
| 85 | object.trigger('orderstoplock.orderable', [item]); |
||
| 86 | item.addClass('locked'); |
||
| 87 | setTimeout(function() { |
||
| 88 | item.removeClass('locked'); |
||
| 89 | object.trigger('orderstopunlock.orderable', [item]); |
||
| 90 | }, settings.delay); |
||
| 91 | } |
||
| 92 | }); |
||
| 93 | |||
| 94 | // Order items |
||
| 95 | $(document).on('mousemove.orderable', '.orderable:has(.ordering)', function order(event) { |
||
| 96 | var object = $(this); |
||
| 97 | if (object.data('ordering') != 1) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | // Only keep what we need from event object |
||
| 101 | var pageY = event.pageY; |
||
| 102 | Symphony.Utilities.requestAnimationFrame(function () { |
||
| 103 | var item = object.find('.ordering'); |
||
| 104 | |||
| 105 | // If there is still an ordering item in DOM |
||
| 106 | if (!item.length) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | var top = item.offset().top, |
||
| 111 | bottom = top + item.outerHeight(), |
||
| 112 | prev, next; |
||
| 113 | |||
| 114 | // Remove text ranges |
||
| 115 | if(window.getSelection) { |
||
| 116 | window.getSelection().removeAllRanges(); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Move item up |
||
| 120 | if(pageY < top) { |
||
| 121 | prev = item.prev(settings.items); |
||
| 122 | if(prev.length > 0) { |
||
| 123 | item.insertBefore(prev); |
||
| 124 | object.trigger('orderchange', [item]); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // Move item down |
||
| 129 | else if(pageY > bottom) { |
||
| 130 | next = item.next(settings.items); |
||
| 131 | if(next.length > 0) { |
||
| 132 | item.insertAfter(next); |
||
| 133 | object.trigger('orderchange', [item]); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | }); |
||
| 137 | }); |
||
| 138 | |||
| 139 | /*------------------------------------------------------------------------- |
||
| 140 | Initialisation |
||
| 141 | -------------------------------------------------------------------------*/ |
||
| 142 | |||
| 143 | // Make orderable |
||
| 144 | objects.addClass('orderable'); |
||
| 145 | |||
| 146 | /*-----------------------------------------------------------------------*/ |
||
| 147 | |||
| 148 | return objects; |
||
| 149 | }; |
||
| 150 | |||
| 152 |